// src/store/slices/consultSlice.ts
import { createSlice, Draft, PayloadAction } from "@reduxjs/toolkit";
import { persistReducer } from "redux-persist";
import storage from "redux-persist/lib/storage";
import {AppState} from "@store/index";

export interface ConsultState {
    consult: PartialConsult;
}

export const consultSlice = createSlice<
    ConsultState,
    {
        // 记录问诊信息
        saveConsult(
            state: Draft<ConsultState>,
            action: PayloadAction<PartialConsult>
        ): void;
        // 清空问诊记录
        clearConsult(state: Draft<ConsultState>): void;
    },
    "consult"
    >({
    name: "consult",
    initialState: {
        // 问诊信息
        consult: {},
    },
    reducers: {
        // 记录问诊信息
        saveConsult(state, action) {
            state.consult = {
                ...state.consult,
                ...action.payload,
            };
        },
        // 清空问诊记录
        clearConsult(state) {
            state.consult = {};
        },
    },
});

// 持久化
export const consultReducer = persistReducer(
    { key: consultSlice.name, storage },
    consultSlice.reducer
);
// 获取问诊信息
export const selectConsult = (state: AppState) => state.consult.consult;
export const { saveConsult, clearConsult } = consultSlice.actions;